﻿package src.tools {
	import flash.text.TextField;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.display.MovieClip;
	import flash.display.LoaderInfo;
	
	public class Preloader extends MovieClip {
		private var dataTextField:TextField;
		
		public function Preloader() {
			stop();	
		}
		
		public function setLoaderInfo(ldr:LoaderInfo):void {
			ldr.addEventListener(ProgressEvent.PROGRESS, onProgress);
			ldr.addEventListener(Event.COMPLETE, onComplete);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, onError);
			play();
		}
		
		private function onProgress(event:ProgressEvent):void {
			var loadedKb:int = Math.round(event.target.bytesLoaded / 1024);
			var totalKb:int = Math.round(event.target.bytesTotal / 1024);
			var percent:int = Math.round(loadedKb/totalKb * 100);
			info.text = String(percent + "%");
		}
		
		private function onComplete(event:Event):void {
			event.target.removeEventListener(ProgressEvent.PROGRESS, onProgress);  
     		event.target.removeEventListener(Event.COMPLETE, onComplete); 
			event.target.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			dispatchEvent(new Event(Event.COMPLETE));	
		}
		
		private function onError(event:IOErrorEvent):void {
			event.target.removeEventListener(ProgressEvent.PROGRESS, onProgress);  
     		event.target.removeEventListener(Event.COMPLETE, onComplete); 
			event.target.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			dispatchEvent(new Event(IOErrorEvent.IO_ERROR));	
		}
	}
}